package com.tsingteng.cosfun.ui.search;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.bean.SearchUserResp;
import com.tsingteng.cosfun.utils.ImageUtils;
import com.tsingteng.cosfun.utils.StringUtils;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class SearchInitAdapter extends BaseAdapter{
    private Context mContext;
    private List<SearchUserResp> mListRecommen;
    private FocusSearchListener mAttentListener;
//    private final int FOOTERLAYOUT = 100;//加载更多
//    private int viewType = 0;
    public SearchInitAdapter(Context context, List<SearchUserResp> listRecommen, FocusSearchListener listener){
        this.mContext = context;
        this.mListRecommen = listRecommen;
        this.mAttentListener = listener;
    }
    @Override
    public int getCount() {
        if (mListRecommen.size()%4 == 0) {
            return mListRecommen.size()/4;
        }else{
            return mListRecommen.size()/4+1;
        }
    }

//    @Override
//    public int getItemViewType(int position) {
//        if (getCount() > 0 && position == getCount() - 1) {
//            viewType = FOOTERLAYOUT;
//            return FOOTERLAYOUT;
//        } else {
//            viewType = 0;
//            return 0;
//        }
//    }

    @Override
    public SearchUserResp getItem(int i) {
        return mListRecommen.get(i);
    }

    @Override
    public long getItemId(int i) {
        return mListRecommen.size();
    }

    @Override
    public View getView(int positon, View view, ViewGroup viewGroup) {
//        if (viewType == FOOTERLAYOUT){
//            view = LayoutInflater.from(mContext).inflate(R.layout.search_listview_footer, viewGroup, false);
//            return view;
//        }
        ViewHolder viewHolder;
        if (view ==null){
            viewHolder = new ViewHolder();
            view = LayoutInflater.from(mContext).inflate(R.layout.item_search_layout,viewGroup,false);
            viewHolder.mFoucsOne = view.findViewById(R.id.focus_iv);
            viewHolder.mFoucsName = view.findViewById(R.id.focus_name);
            viewHolder.mFanNum = view.findViewById(R.id.fans_num);
            viewHolder.mFoucsTv = view.findViewById(R.id.focus_tv);
            viewHolder.mFoucsTwoIv = view.findViewById(R.id.focus_two_iv);
            viewHolder.mFoucsTwoName = view.findViewById(R.id.focus_two_names);
            viewHolder.mFanTwoNum = view.findViewById(R.id.fans_two_num);
            viewHolder.mFoucsTwoTv = view.findViewById(R.id.focus_two_tv);
            viewHolder.mFoucsThreeIv = view.findViewById(R.id.focus_three_iv);
            viewHolder.mFocusThreeName = view.findViewById(R.id.focus_three_name);
            viewHolder.mFanThreeNum = view.findViewById(R.id.fans_three_num);
            viewHolder.mFocusThreeTv = view.findViewById(R.id.focus_three_tv);
            viewHolder.line=view.findViewById(R.id.line_ll);
            viewHolder.lin_foucs_one=view.findViewById(R.id.lin_foucs_one);
            viewHolder.lin_foucs_two=view.findViewById(R.id.lin_foucs_two);
            viewHolder.lin_foucs_three=view.findViewById(R.id.lin_foucs_three);
            viewHolder.lin_foucs_four = view.findViewById(R.id.lin_foucs_four);
            viewHolder.mFoucsFourIv = view.findViewById(R.id.focus_four_iv);
            viewHolder.mFocusFourName = view.findViewById(R.id.focus_four_name);
            viewHolder.mFanFourNum = view.findViewById(R.id.fans_four_num);
            viewHolder.mFocusFourTv = view.findViewById(R.id.focus_four_tv);
            view.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder)view.getTag();
        }
        if (positon*4 < mListRecommen.size()) {
            SearchUserResp atten = mListRecommen.get(positon * 4);
            viewHolder.lin_foucs_one.setVisibility(View.VISIBLE);
            ImageUtils.LoadImage(mContext, atten.getProfileAvatar(), viewHolder.mFoucsOne);
            viewHolder.mFoucsName.setText(atten.getProfileName());
            String fans = StringUtils.formatZanCount(Integer.parseInt(atten.getNumberOfFans()));
            String fan = fans+mContext.getResources().getString(R.string.fans);
            viewHolder.mFanNum.setText(fan);
            if (1 == atten.getIsAttention()){
                viewHolder.mFoucsTv.setText(mContext.getResources().getString(R.string.focused));
                viewHolder.mFoucsTv.setTextColor(mContext.getResources().getColor(R.color.color_found));
                viewHolder.mFoucsTv.setBackground(mContext.getResources().getDrawable(R.drawable.shape_bg_focused));
            }else{
                viewHolder.mFoucsTv.setText(mContext.getResources().getString(R.string.focus));
                viewHolder.mFoucsTv.setTextColor(mContext.getResources().getColor(R.color.black));
                viewHolder.mFoucsTv.setBackground(mContext.getResources().getDrawable(R.drawable.shape_logout));
            }
            viewHolder.mFoucsTv.setOnClickListener(view1 -> mAttentListener.searchFocus(positon * 4,atten));
            viewHolder.mFoucsOne.setOnClickListener(view12 -> mAttentListener.clickImage(positon*4,atten));
        }else{
            viewHolder.lin_foucs_one.setVisibility(View.INVISIBLE);
        }
        if (positon*4+1 < mListRecommen.size()){
            viewHolder.lin_foucs_two.setVisibility(View.VISIBLE);
            SearchUserResp atten = mListRecommen.get(positon*4+1);
            ImageUtils.LoadImage(mContext,atten.getProfileAvatar(),viewHolder.mFoucsTwoIv);
            viewHolder.mFoucsTwoName.setText(atten.getProfileName());
            String fans = StringUtils.formatZanCount(Integer.parseInt(atten.getNumberOfFans()));
            String fan = fans+mContext.getResources().getString(R.string.fans);
            viewHolder.mFanTwoNum.setText(fan);
            if (1 == atten.getIsAttention()){
                viewHolder.mFoucsTwoTv.setText(mContext.getResources().getString(R.string.focused));
                viewHolder.mFoucsTwoTv.setTextColor(mContext.getResources().getColor(R.color.color_found));
                viewHolder.mFoucsTwoTv.setBackground(mContext.getResources().getDrawable(R.drawable.shape_bg_focused));
            }else{
                viewHolder.mFoucsTwoTv.setText(mContext.getResources().getString(R.string.focus));
                viewHolder.mFoucsTwoTv.setTextColor(mContext.getResources().getColor(R.color.black));
                viewHolder.mFoucsTwoTv.setBackground(mContext.getResources().getDrawable(R.drawable.shape_logout));
            }
            viewHolder.mFoucsTwoTv.setOnClickListener(view13 -> mAttentListener.searchFocus(positon*4+1,atten));
            viewHolder.mFoucsTwoIv.setOnClickListener(view14 -> mAttentListener.clickImage(positon*4+1,atten));
        }else{
            viewHolder.lin_foucs_two.setVisibility(View.INVISIBLE);
        }
        if (positon*4+2< mListRecommen.size()) {
            SearchUserResp atten = mListRecommen.get(positon*4+2);
            viewHolder.lin_foucs_three.setVisibility(View.VISIBLE);
            ImageUtils.LoadImage(mContext,atten.getProfileAvatar(),viewHolder.mFoucsThreeIv);
            viewHolder.mFocusThreeName.setText(atten.getProfileName());
            String fans = StringUtils.formatZanCount(Integer.parseInt(atten.getNumberOfFans()));
            String fan = fans+mContext.getResources().getString(R.string.fans);
            viewHolder.mFanThreeNum.setText(fan);
            if (1 == atten.getIsAttention()){
                viewHolder.mFocusThreeTv.setText(mContext.getResources().getString(R.string.focused));
                viewHolder.mFocusThreeTv.setTextColor(mContext.getResources().getColor(R.color.color_found));
                viewHolder.mFocusThreeTv.setBackground(mContext.getResources().getDrawable(R.drawable.shape_bg_focused));
            }else{
                viewHolder.mFocusThreeTv.setText(mContext.getResources().getString(R.string.focus));
                viewHolder.mFocusThreeTv.setTextColor(mContext.getResources().getColor(R.color.black));
                viewHolder.mFocusThreeTv.setBackground(mContext.getResources().getDrawable(R.drawable.shape_logout));
            }
            viewHolder.mFocusThreeTv.setOnClickListener(view15 -> mAttentListener.searchFocus(positon*4+2,atten));
            viewHolder.mFoucsThreeIv.setOnClickListener(view16 -> mAttentListener.clickImage(positon*4+2,atten));
        }else{
            viewHolder.lin_foucs_three.setVisibility(View.INVISIBLE);
        }
        if (positon*4+3<mListRecommen.size()){
            SearchUserResp atten = mListRecommen.get(positon*4+3);
            viewHolder.lin_foucs_four.setVisibility(View.VISIBLE);
            ImageUtils.LoadImage(mContext,atten.getProfileAvatar(),viewHolder.mFoucsFourIv);
            viewHolder.mFocusFourName.setText(atten.getProfileName());
            String fans = StringUtils.formatZanCount(Integer.parseInt(atten.getNumberOfFans()));
            String fan = fans+mContext.getResources().getString(R.string.fans);
            viewHolder.mFanFourNum.setText(fan);
            if (1 == atten.getIsAttention()){
                viewHolder.mFocusFourTv.setText(mContext.getResources().getString(R.string.focused));
                viewHolder.mFocusFourTv.setTextColor(mContext.getResources().getColor(R.color.color_found));
                viewHolder.mFocusFourTv.setBackground(mContext.getResources().getDrawable(R.drawable.shape_bg_focused));
            }else{
                viewHolder.mFocusFourTv.setText(mContext.getResources().getString(R.string.focus));
                viewHolder.mFocusFourTv.setTextColor(mContext.getResources().getColor(R.color.black));
                viewHolder.mFocusFourTv.setBackground(mContext.getResources().getDrawable(R.drawable.shape_logout));
            }
            viewHolder.mFocusFourTv.setOnClickListener(view17 -> mAttentListener.searchFocus(positon*4+3,atten));
            viewHolder.mFoucsFourIv.setOnClickListener(view18 -> mAttentListener.clickImage(positon*4+3,atten));
        }else{
            viewHolder.lin_foucs_four.setVisibility(View.INVISIBLE);
        }
        return view;
    }
    class ViewHolder{
        LinearLayout line;
        LinearLayout lin_foucs_one;
        LinearLayout lin_foucs_two;
        LinearLayout lin_foucs_three;
        LinearLayout lin_foucs_four;
        CircleImageView mFoucsOne;
        TextView mFoucsName;
        TextView mFanNum;
        TextView mFoucsTv;
        CircleImageView mFoucsTwoIv;
        TextView mFoucsTwoName;
        TextView mFanTwoNum;
        TextView mFoucsTwoTv;
        CircleImageView mFoucsThreeIv;
        TextView mFocusThreeName;
        TextView mFanThreeNum;
        TextView mFocusThreeTv;
        CircleImageView mFoucsFourIv;
        TextView mFocusFourName;
        TextView mFanFourNum;
        TextView mFocusFourTv;
    }
   public interface  FocusSearchListener{
        void searchFocus(int position, SearchUserResp bean);
        void clickImage(int position, SearchUserResp bean);
    }
}
